/**
 * 
 */
package com.suyx.job.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.suyx.job.entity.DeviceInfo;

/**
 * @author Administrator
 *
 */
public class CommandUtils {
	
	public static String excuteCommand(String cmd){
		/* 获取cmd命令 */
		try
		{
			System.out.println("正在执行命令：" + cmd);
			Process pro = Runtime.getRuntime().exec(cmd); // 添加要进行的命令，"cmd
															// /c
															// calc"中calc代表要执行打开计算器，如何设置关机请自己查找cmd命令

			BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); // 虽然cmd命令可以直接输出，但是通过IO流技术可以保证对数据进行一个缓冲。
			String msg = null;
			String result = null;
			System.out.println("执行完成！！！");
			while ((msg = br.readLine()) != null){
				System.out.println(msg);
				if (result == null)
					result = msg;
			}
			System.out.println(result);
			return result;
		} catch (IOException exception){
			exception.printStackTrace();
			return "";
		}
	}
	
	public static String getDevices(){
		/* 获取cmd命令 */
		try{
			Process pro = Runtime.getRuntime().exec("adb devices"); 
			BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); // 虽然cmd命令可以直接输出，但是通过IO流技术可以保证对数据进行一个缓冲。
			String msg = null;
			StringBuffer devices = new StringBuffer();
			while ((msg = br.readLine()) != null){
				System.out.println(msg);
				if(msg.startsWith("List")){
					continue;
				}
				devices.append("&");
				devices.append(msg.replaceAll("\\t","@"));
			}
			return devices.toString();
		} catch (IOException exception){
			exception.printStackTrace();
			return "";
		}
	}
	
	public static String appProcessExcuteCommand(String device){
		String cmd = "adb -s " + device + " shell CLASSPATH=/sdcard/Main.dex   app_process /sdcard com.wanjian.puppet.Main";
		try{
			System.out.println("正在执行命令：" + cmd);
			Process pro = Runtime.getRuntime().exec(cmd); 
			BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); // 虽然cmd命令可以直接输出，但是通过IO流技术可以保证对数据进行一个缓冲。
			String msg = null;
			String result = null;
			System.out.println("执行完成！！！");
			while ((msg = br.readLine()) != null){
				System.out.println(msg);
				if (result == null)
					result = msg;
			}
			System.out.println(result);
			return result;
		} catch (IOException exception){
			exception.printStackTrace();
			return "";
		}
	}
	
	public static String forwardExcuteCommand(String device,String port){
		String cmd = "cmd /k start adb -s " + device + " forward tcp:" + port + " localabstract:puppet-ver1";
		try{
			System.out.println("正在执行命令：" + cmd);
			Process pro = Runtime.getRuntime().exec(cmd); 
															
			BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); // 虽然cmd命令可以直接输出，但是通过IO流技术可以保证对数据进行一个缓冲。
			String msg = null;
			String result = null;
			System.out.println("执行完成！！！");
			while ((msg = br.readLine()) != null){
				System.out.println(msg);
				if (result == null)
					result = msg;
			}
			System.out.println(result);
			return result;
		} catch (IOException exception){
			exception.printStackTrace();
			return "";
		}
	}
	
	public static void main(String[] args) {
		String device = "08ed0cbd7d2a";
		
		//String device = " 0d5e784c7d32";
		String result = CommandUtils.appProcessExcuteCommand(device);
		System.out.println(result);
		String result1 = CommandUtils.forwardExcuteCommand(device, "8888");
		//String result = CommandUtils.excuteCommand("adb shell CLASSPATH=/sdcard/Main.dex   app_process /sdcard com.wanjian.puppet.Main");
		//System.out.println(result);
		//String result1 = CommandUtils.excuteCommand("cmd /k start adb forward tcp:8888 localabstract:puppet-ver1");
		System.out.println(result1);
	}
	public static void main22(String[] args) throws InterruptedException {
		String device = "08ed0cbd7d2a";
		CommandUtils.excuteCommand("adb -s 08ed0cbd7d2a shell am force-stop com.jifen.qukan");
		Thread.sleep(1000l);
		CommandUtils.excuteCommand("adb -s 08ed0cbd7d2a shell am  start -n com.jifen.qukan/com.jifen.qkbase.main.MainActivity");
		Thread.sleep(5000l);
		int i = 0;
   		while(i < 30){
   		    String cmd = "adb -s "+device +" shell input swipe 375 824 375 643 500";
   			CommandUtils.excuteCommand(cmd);
   			i++;
   			if(i%3==0){
   				cmd = "adb -s "+device +" shell input tap 375 824";
       			CommandUtils.excuteCommand(cmd);
       			int j = 0;
       			while(j < 10){
       				cmd = "adb -s "+device +" shell input swipe 375 824 375 643 500";
	       			CommandUtils.excuteCommand(cmd);
	       			if(j == 9){
	       				cmd = "adb -s "+device +" shell input keyevent 4";
		       			CommandUtils.excuteCommand(cmd);
	       			}
	       			j++;
       			}
   			}
   			
   		}
	}
	
	public static void main8(String[] args) throws InterruptedException {
		CommandUtils.excuteCommand("adb -s 0d5e784c7d32 shell am force-stop com.netease.mhxyhtb");
	}
	
	public static void main1(String[] args) throws InterruptedException {
		CommandUtils.excuteCommand("adb shell dumpsys package  com.lechuan.mdwz");
	}
	
}
